<?php
class AuthController extends OController {

    public $layout = 'loginLayout';
    public $defaultAction = 'login';

    public function actionLogin() {
        
        $model = new LoginForm('login');
        $this->performAjaxValidation($model);

        $params = Yii::app()->request->getParam(get_class($model));
        if ($params) {
            $model->attributes=$params;
            if ($model->validate()) {
                $identity = new UserIdentity($model->username_, $model->password_);
                if($identity->authenticate(UserIdentity::ADMIN)){
                    Yii::app()->user->login($identity, $model->rememberMe_? Yii::app()->params['cookieLoginDuration']:0); // cookie-based login 7 days
                    $user=Yii::app()->user->loadModel();
                    Yii::app()->setLanguage($user->preferredLanguageCode);
                    $this->redirect(array('site/index'));
                }else{
                    $model->addError('error', $identity->getErrorMessage());
                }
            }//end if
        }//end if else

        $this->render('loginForm', array(
            'model' => $model,
            'formConfig' => array(
                'id' => $this->formId,
                'enableAjaxValidation' => true,
                'enableClientValidation' => true,
                'clientOptions' => array(
                    'validateOnSubmit' => true,
                ),
                'focus' => array($model, 'username_'),
            ),
        ));
    }

}//end class